package com.zbl.springdemo.mq.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Author ZhuBoLin
 * @Data 2021/5/7 21:46
 * @Version
 **/

//@Component
public class MyAckReceiver implements ChannelAwareMessageListener {
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            // 因为传递消息的时候用的map传递,所以将Map从Message内取出需要做些处理
            String  msgBody = new String (message.getBody());
            MessageProperties messageProperties = message.getMessageProperties();

            System.out.println("消费的主题消息来自：" + message.getMessageProperties().getConsumerQueue());
            channel.basicAck(deliveryTag, true); // 1、消息id；2、true 时，一次性确认小于此id的消息
        } catch (Exception e) {
            channel.basicNack(deliveryTag, false, false);// 1、消息id；2、true，拒绝所有小于此id的消息；3、true 重新入列
            channel.basicReject(deliveryTag, false);// 1、消息id; 2、true 重新入列
            e.printStackTrace();
        }
    }
}
